@extends('layouts.app')
@include('good.sidebar')
@section('content')
    <div class="content">
        <a class="btn btn-info btn-sm" href="{{ redirect()->back()->getTargetUrl() }}"> 返回 </a>
        <br />
        <br />
        {!! form($form) !!}
    </div>
@endsection
@push('scripts')
<script>

    // 按照客户要求, 修改表单样式
    // 表单通过后台生成, 所以通过js调整样式
    var _form_type = "{{isset($flag) ? $flag : ''}}";
    
    if (_form_type == 'goods') {
        $('form').attr('autocomplete', 'off')
        // $('.content-header ul').find('.active').find('a').text('商品详情')
        $('.content').find('form').addClass('form-inline')
        $('.content').find('form').find('label').attr('style', 'width:80px;text-align:center')
        $('.form-group').attr('style', 'margin:10px 0px;')

        $('[name="name"]').attr('style', 'width:1220px;')
        $('[name="name"]').parent().after('<br>')

        $('[name="top_category_id"]').attr('style', 'width:239px;')
        $('[name="first_category_id"]').attr('style', 'width:239px;')
        $('[name="category_id"]').attr('style', 'width:239px;')
        $('[name="brand_id"]').attr('style', 'width:239px;')
        $('[name="brand_id"]').parent().after('<br>')

        $('[name="display_price"]').attr('style', 'width:130px;')
        $('[name="price"]').attr('style', 'width:130px;')
        $('[name="tag"]').attr('style', 'width:130px;')
        $('[name="keywords"]').attr('style', 'width:130px;')
        $('[name="virtual_sales_volume"]').attr('style', 'width:130px;')
        $('[name="sort"]').attr('style', 'width:130px;')
        $('[name="sort"]').parent().after('<br>')

        $('[for="tag_id[]"]').parent().after('<br>')

        $('[for="cover"]').next('div').attr('style', 'display:inline')
        $('[for="cover"]').parent().find('br').remove();
        $('[for="cover"]').parent().find('p').attr('style', 'margin-left:10px;display:inline');
        $('[for="cover"]').parent().find('p').after('<br>');
        $('[for="cover"]').parent().after('<br>');

        $('[for="order_cover"]').next('div').attr('style', 'display:inline')
        $('[for="order_cover"]').parent().find('br').remove();
        $('[for="order_cover"]').parent().find('p').attr('style', 'margin-left:10px;display:inline');
        $('[for="order_cover"]').parent().find('p').after('<br>');
        $('[for="order_cover"]').parent().after('<br>');

        $('[for="list_cover"]').next('div').attr('style', 'display:inline')
        $('[for="list_cover"]').parent().find('br').remove();
        $('[for="list_cover"]').parent().find('p').attr('style', 'margin-left:10px;display:inline');
        $('[for="list_cover"]').parent().find('p').after('<br>');
        $('[for="list_cover"]').parent().after('<br>');

        $('[for="video"]').next('div').attr('style', 'display:inline')
        $('[for="video"]').parent().find('br').remove();
        $('[for="video"]').parent().find('p').attr('style', 'margin-left:10px;display:inline');
        $('[for="video"]').parent().find('p').after('<br>');
        $('[for="video"]').parent().after('<br>');

        $('[for="share_bg"]').next('div').attr('style', 'display:inline')
        $('[for="share_bg"]').parent().find('br').remove();
        $('[for="share_bg"]').parent().find('p').attr('style', 'margin-left:10px;display:inline');
        $('[for="share_bg"]').parent().find('p').after('<br>');
        $('[for="share_bg"]').parent().after('<br>');

        $('[for="is_free"]').attr('style', 'width:190px;height:34px;line-height:30px')
        $('[for="is_balance_pay"]').attr('style', 'width:190px;height:34px;line-height:30px')
        $('[for="is_recharge"]').attr('style', 'width:190px;height:34px;line-height:30px')
        $('[for="recharge_money"]').attr('style', 'width:90px;height:34px;line-height:30px;margin-left:-20px;')
        $('[for="recharge_money"]').parent().find('input').attr('style', 'width:100px;margin-right:20px')
        // $('[for="recharge_money"]').parent().after('<br>');

        $('[for="buy_limit"]').parent().find('p').attr('style', 'margin-left:10px;margin-right:50px;display:inline');
        $('[for="buy_limit"]').parent().find('input').attr('style', 'width:150px;')
        $('[for="end_time"]').attr('style', 'width:100px;')
        $('[for="end_time"]').parent().find('input').attr('style', 'width:150px;')
        $('[for="end_time"]').parent().after('<br>');

        $('[for="description"]').parent().after('<br>');
        $('[for="spec"]').parent().after('<br>');
    }

</script>
    <script>
        // $("#end_time").datetimepicker({format:"YYYY-MM-DD HH:mm:ss"});
        $("#end_time").datetimepicker({format: "Y-m-d H:i:00", step:10});
        $.datetimepicker.setLocale('zh');
        tinymce.init({
            selector: '#editor, #editor1',
            plugins: "textcolor colorpicker uploadimage autoresize",
            menubar: false,
            toolbar: "uploadimage undo redo | fontsizeselect bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify",
            images_upload_url: '/admin/upload_image?_token=' + $('meta[name="csrf-token"]').attr('content'),
            images_upload_credentials: true,
            image_dimensions: false,
            image_caption: false,
            image_description: false,
            language: 'zh_CN',
            language_url : '/tinymce/langs/zh_CN.js',
            autoresize_min_height: 350,
            autoresize_max_height: 800,
            content_style: 'img {width: 100%; height: auto; vertical-align: middle;}',
            init_instance_callback: function (editor) {
                editor.on('change', function (e) {
                    editor.save();
                });
            }
        });
        
        // 选择顶级分类  查询对应一级分类和品牌
        $('#top_category_id').on('change', function () {

            top_category_id = $(this).val()
            shop_id = {{ $shopId }}
            // 一级分类
            $.ajax({
                url: '{{ route('admin.good.select_category') }}',
                type: "GET",
                dataType:'json',
                data: {
                    top_category_id: top_category_id,
                    first_category_id:0,
                    shop_id : shop_id
                },
                success: function (re) {
                    $('#first_category_id').empty()
                    options = '';
                    for (var i = 0; i < re.length; i++) {
                        options += '<option value="' + re[i].id + '">' + re[i].name + '</option>'
                    }
                    $('#first_category_id').append(options)
                }
            });

            // 品牌
            $.ajax({
                url: '{{ route('admin.good.select_brand') }}',
                type: "GET",
                dataType:'json',
                data: {
                    pid: top_category_id,
                    shop_id : shop_id
                },
                success: function (re) {
                    $('#brand_id').empty()
                    options = '';
                    for (var i = 0; i < re.length; i++) {
                        options += '<option value="' + re[i].id + '">' + re[i].name + '</option>'
                    }
                    $('#brand_id').append(options)
                }
            });
        });

        get_second_category()
        // 选择一级分类 查询二级分类
        $('#first_category_id').on('change', function () {
            $('#second_category-select').empty()
            get_second_category()
            
        });

        function get_second_category(){
            top_category_id = $('#top_category_id').find('option:selected').val()
            first_category_id = $('#first_category_id').find('option:selected').val()
            shop_id = {{ $shopId }}

            url = '{{ route('admin.good.select_second_category') }}'+'?top_category_id='+top_category_id+'&first_category_id='+first_category_id+'&shop_id='+shop_id

            second_category = $("#second_category-select").select2({
                width: "239px",
                ajax: {
                    dataType: 'json',
                    delay: 250,
                    url: url,
                    processResults: function (data) {
                        return {
                            results: $.map(data, function(row, key) {
                                return {
                                    id: key,
                                    text: row
                                };
                            })
                        };
                    }
                }
            });
        }

        get_tag()
        function get_tag(){
            shop_id = {{ $shopId }}
            url = '{{ route('admin.tag.select') }}'+'?shop_id='+shop_id

            tag_id = $("#tag_id-select").select2({
                width: "550px",
                ajax: {
                    dataType: 'json',
                    delay: 250,
                    url: url,
                    processResults: function (data) {
                        return {
                            results: $.map(data, function(row, key) {

                                return {
                                    id: key,
                                    text: row
                                };
                            })
                        };
                    },
                }
            });
        }

        @if ( empty($is_recharge) )
            $('#recharge_money').attr("disabled", true);
        @endif

        $('#is_recharge').click(function(){
            if ($(this).prop("checked"))
                $('#recharge_money').attr("disabled", false);
            else
                $('#recharge_money').attr("disabled", true);
        })

    </script>
@endpush
